import { Table} from 'antd';
import React from 'react'

export default class RecordTable extends React.Component{
    constructor(props){
        console.info(props);
        super(props);
        this.state={
            dataSource:[],
        };

        this.updateTable = this.updateTable.bind(this);
    }

    updateTable(){
        fetch("http://localhost:80/search/getRcdList?id="+this.props.stuId,{
            method: 'GET',
            headers: Headers,
            'mode': 'cors'
        })
            .then(res => {
                console.log(res);
                return res.json();
            })
            .then(data => {

                for(let i=0;i<data.length;i++){
                    data[i]["key"] = data[i].id;
                }
                console.info(data);
                this.setState({dataSource:data});
            });
    }

    componentDidMount(){
        this.updateTable();
    }

    componentWillUnmount(){

    }


    render(){

        const columns = [
            {
                title: '学号',
                dataIndex: 'studentId',
                key: 'studentId',
            },
            {
                title: '姓名',
                dataIndex: 'name',
                key: 'name',
            },
            {
                title: '金额',
                dataIndex: 'variation',
                key: 'variation',
                render: (text, record, index)=>{
                    if(record.variation>0)
                        return"+"+record.variation;
                    else
                        return ""+record.variation;
                }
            },
            {
                title: '剩余金额',
                dataIndex: 'aftermoney',
                key: 'aftermoney',
            },
            {
                title: '时间',
                dataIndex: 'time',
                key: 'time',
                render: (text, record, index)=>{
                    console.info(new Date(record.time));
                    return new Date(record.time).toLocaleDateString()+" "+new Date(record.time).toLocaleTimeString();
                }
            },
            {
                title: '地点',
                dataIndex: 'place',
                key: 'place',
            },
        ];

        return (
            <div>
                <Table dataSource={this.state.dataSource} columns={columns}/>
            </div>
        )
    }
}